package models

import (
	"guard/internal/app/oauth/models"
	"guard/internal/tool"
)

type ClientInfo struct {
	models.OAuth2RegisteredClient
}

// ClientDto client的映射，用于接收和返回数据
type ClientDto struct {
	Id           string `json:"id,omitempty"`
	ClientId     string `json:"client_id,omitempty"`
	ClientName   string `json:"client_name,omitempty"  binding:"required"`
	ClientSecret string `json:"client_secret,omitempty"  binding:"required"`

	// 三种模式authorization_code，refresh_token，client_credentials
	AuthorizationGrantTypes []string `json:"authorization_grant_types,omitempty"`
	RedirectUris            string   `json:"redirect_uris,omitempty" binding:"required"`
	Scopes                  []string `json:"scopes,omitempty"`

	// 时间戳，秒级别
	AuthorizationCodeExpires int64 `json:"authorization_code_expires,omitempty"` // 默认5分钟，5*60
	AccessTokenExpires       int64 `json:"access_token_expires,omitempty"`       // 默认24小时，24*60*60
	RefreshTokenExpires      int64 `json:"refresh_token_expires,omitempty"`      // 默认7天，7*24*60*60
}

// NewClientDto 用于转换查询到的对象，展示给前端
func NewClientDto(clientInfo *ClientInfo) *ClientDto {
	return &ClientDto{
		Id:                       clientInfo.ClientId,
		ClientId:                 clientInfo.ClientId,
		ClientName:               clientInfo.ClientName,
		ClientSecret:             "***",
		AuthorizationGrantTypes:  tool.StringListDestroy(clientInfo.AuthorizationGrantTypes),
		RedirectUris:             clientInfo.RedirectUris,
		Scopes:                   tool.StringListDestroy(clientInfo.Scopes),
		AuthorizationCodeExpires: clientInfo.AuthorizationCodeExpires,
		AccessTokenExpires:       clientInfo.AccessTokenExpires,
		RefreshTokenExpires:      clientInfo.RefreshTokenExpires,
	}
}

func (cdt *ClientDto) Serialization() *ClientInfo {
	ci := &ClientInfo{}
	ci.Id = cdt.Id
	ci.ClientId = cdt.ClientId
	ci.ClientName = cdt.ClientName
	ci.ClientSecret = cdt.ClientSecret
	ci.AuthorizationGrantTypes = tool.StringListBuild(cdt.AuthorizationGrantTypes)
	ci.RedirectUris = cdt.RedirectUris
	ci.Scopes = tool.StringListBuild(cdt.Scopes)
	ci.AuthorizationCodeExpires = cdt.AuthorizationCodeExpires
	ci.AccessTokenExpires = cdt.AccessTokenExpires
	ci.RefreshTokenExpires = cdt.RefreshTokenExpires
	return ci
}

// UserApp 用户应用，生成的信息将自动添加到上面结构体
type UserApp struct {
	UserName     string `json:"user_name,omitempty"`
	ClientId     string `json:"client_id,omitempty"`
	ClientSecret string `json:"client_secret,omitempty"`
	ClientName   string `json:"client_name,omitempty"`
}

func (UserApp) TableName() string {
	return "user_app"
}
